package cz.masa.FotoCAT;

import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

// slozka (group) ve stromu ve ktere jsou zarazeny podslozky, resp. filmy

class SkupinaPolozka extends treePolozka{
    private fcaGroup group;

    public SkupinaPolozka(fcaGroup group){
        this.group = group;
        createPopup();
    }

    public String toString(){
        return group.getName();
    }    
    
    private void paste(){
        if(FotoCAT.clipboard instanceof fcaDirectory){
            ((fcaDirectory)FotoCAT.clipboard).setGroup(group.getId());
            FotoCAT.clipboard = null;
        }
        FotoCAT.rereadDirPan();
    }
    
    private void createPopup(){
        JMenuItem menuItem;
        popup = new JPopupMenu();
        
        menuItem = new JMenuItem("Informations");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { showInfo(); }
        });
        popup.add(menuItem);        
        
        menuItem = new JMenuItem("Rename group");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { prejmenovat(); }
        });
        popup.add(menuItem);

        menuItem = new JMenuItem("Delete group");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { smazat(); }
        });
        popup.add(menuItem);

        menuItem = new JMenuItem("Add new subgroup");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { SkupinaPolozka.nova(group.getId()); }
        });
        popup.add(menuItem);
        
        popup.add(new JPopupMenu.Separator());
        
        menuItem = new JMenuItem("Move selected here");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { paste(); }
        });
        popup.add(menuItem);        
    }

    public String getInfo(int style){
        String ret = "";

        if(style == INFO_UTREE){
            ret += "Name:  " + group.getName() + "\n";
        }

        if(style == INFO_FULL){
            ret += "Name:  " + group.getName() + "\n";
            ret += "Directories (films) count:  " + group.getDirectoryCount() + "\n";
            ret += "Number of direct sub-groups:  " + group.getNumberOfSubGroups() + "\n";
            ret += "Amount of data in directories:  " + FotoCATutil.getSizeString(group.getDataAmount());
        }        

        return ret;
    }    
    
    public void smazat(){
        if(group.getDirectoryCount() > 0 || group.getNumberOfSubGroups() != 0){
            JOptionPane.showInternalMessageDialog(FotoCAT.desktop,"Group \"" + group.getName() + "\" include directories or sub-groups so it can't be deleted.");
        } else {
            int ret = JOptionPane.showInternalConfirmDialog(FotoCAT.desktop,"Do you realy want to delete group " + group.getName() + "?","Delete group",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
            if(ret == JOptionPane.OK_OPTION){
                group.delete();
                FotoCAT.rereadDirPan();
            }
        }
    }

    public void prejmenovat(){
        JOptionPane dlg = new JOptionPane();
        String ans = (String)dlg.showInternalInputDialog(FotoCAT.desktop, "Enter new name for group " + group.getName(), "Rename group", JOptionPane.QUESTION_MESSAGE, null, null, group.getName());
        if(ans == null) return;
        if(ans.compareTo("")!=0) group.rename(ans);
        FotoCAT.rereadDirPan();
    }
    
    public static void nova(int parentId){
        JOptionPane dlg = new JOptionPane();
        String ans = dlg.showInternalInputDialog(FotoCAT.desktop, "Enter name of new group","New group",JOptionPane.QUESTION_MESSAGE);
        if(ans == null) return;
        if(ans.compareTo("")!=0) fcaGroup.create(parentId, ans);
        FotoCAT.rereadDirPan();
    }	
}